<div class="form-group">
  <label class="col-sm-2 control-label">广告/优选标题</label>
  <div class="col-sm-4">
    <input type="text" class="form-control" name="title" value={$advert.title}>
  </div>
</div>
<div class="form-group">
  <label class="col-sm-2 control-label">广告类型</label>
  <div class="col-sm-4">
    <select name="types" onChange="changeType(this)" class="form-control" id="mySelect">
      <option value="1" <if condition="!$advert or $advert.types eq 1">selected</if>>店铺广告</option>
      <option value="2" <if condition="$advert.types eq 2">selected</if>>商品广告</option>
      <option value="3" <if condition="$advert.types eq 3">selected</if>>小店优选</option>
    </select>
    <p class="help-block">通过商品管理/商家管理列表可查看店铺ID/商品ID</p>
  </div>
</div>
<div class="form-group shop_id" style="<if condition='!$advert || $advert.is_applet eq 1'>display: none</if>"> 
  <label class="col-sm-2 control-label">店铺ID</label>
  <div class="col-sm-4">
    <input type="text" class="form-control" placeholder="请输入店铺ID" name="shop_id" value={$advert.shop_id}>
  </div>
</div>
<div class="form-group product_id" style="<if condition='!$advert || $advert.types neq 2 || $advert.is_applet eq 1'>display: none</if>">
  <label class="col-sm-2 control-label">商品ID</label>
  <div class="col-sm-4">
    <input type="text" class="form-control" placeholder="请输入商品ID" name="product_id" value={$advert.product_id}>
  </div>
</div>

<div class="form-group appid" style="<if condition='!$advert || $advert.is_applet neq 1'>display: none</if>">
  <label class="col-sm-2 control-label">小程序id</label>
  <div class="col-sm-4">
    <input type="text" class="form-control" placeholder="请输入appid" name="appid" value={$advert.appid}>
  </div>
</div>

<div class="form-group appid" style="<if condition='!$advert || $advert.is_applet neq 1'>display: none</if>">
  <label class="col-sm-2 control-label">跳转地址</label>
  <div class="col-sm-4">
    <input type="text" class="form-control" placeholder="例:/pages/index/index?id=123" name="link" value={$advert.link}>
  </div>
</div>

<div class="form-group product_desc" style="<if condition='!$advert || $advert.types eq 1 || $advert.types eq 2'>display: none</if>">
    <label class="col-sm-2 control-label">优选描述</label>
    <div class="col-sm-4">
        <input type="text" class="form-control" placeholder="请输入优选商品描述" name="desc" value={$advert.desc}>
    </div>
</div>
<present name="advert.link">
  <div class="form-group">
    <label class="col-sm-2 control-label">广告/优选链接</label>
    <div class="col-sm-4">
      <input type="text" class="form-control" value={$advert.link} readonly>
    </div>
  </div>
</present>
<div class="form-group">
  <label class="col-sm-2 control-label ">广告/优选图片</label>
  <div class="col-sm-1">
    <div class="uploadimg" id="uploadimg">
      <i class="cm-plus"></i>
      <input type="file" name="photo" data-id="ad-img" class="file-input">
    </div>
  </div>
  <div class="col-sm-1">
    <img id="ad-img" src="{$advert.image}" style=" width:78px; height:78px;">
    <present name="advert.image">
      <input type="hidden" name="image" value={$advert.image}>
    </present>
    
  </div>
  <div class="col-sm-5 tips">
    <p> * 首页默认模式，图片尺寸：640x640 px</p>
    <p> * 首页优选模式，轮播图尺寸：750x350 px</p>
    <p> * 首页优选模式，列表图尺寸：680x360 px</p>
    <p> * <strong>需设置云存储缩略图版本</strong></p>
  </div>
</div>

<div class="form-group">
  <label class="col-sm-2 control-label">跳转小程序</label>
  <div class="col-sm-4">
    <input class="jump" type="radio" value="1" name="is_applet" <if condition="$advert.is_applet eq 1">checked</if> > 是
    <input class="jump" type="radio" value="0" name="is_applet" <if condition="$advert.is_applet eq 0">checked</if> > 否
  </div>
</div>

<div class="form-group">
  <label class="col-sm-2 control-label">是否开启</label>
  <div class="col-sm-4">
    <input type="radio" value="1" name="status" <if condition="$advert.status eq 1">checked</if> > 是
    <input type="radio" value="2" name="status" <if condition="$advert.status eq 2 || $advert.status eq 0">checked</if> > 否
  </div>
</div>
<div class="form-group">
  <label class="col-sm-2 control-label">位置排序</label>
  <div class="col-sm-2">
    <input type="text" class="form-control" name="orderby" value={$advert.orderby}>
  </div>
</div>
<div class="form-group">
  <div class="col-sm-offset-2 col-sm-10">
    <input type="hidden" name="id" value="{$advert.id}">
    <input type="submit" class="btn btn-primary" value="提交">
  </div>
</div>

<script type="text/javascript">
  function GetRequest() {   
     var url = location.search; //获取url中"?"符后的字串   
     var theRequest = new Object();   
     if (url.indexOf("?") != -1) {   
        var str = url.substr(1);   
        strs = str.split("&");   
        for(var i = 0; i < strs.length; i ++) {   
           theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);   
        }   
     }   
     return theRequest;   
  }   

  function changeType(e) {
    var types = e.value;
    var obj=document.getElementsByName("is_applet");
    for (i=0;i<obj.length;i++){  //遍历Radio  
      if(obj[i].checked){
        var index = obj[i].value; //获取Radio的值
      } 
    }

    if(index==0){
        $(".shop_id").show();
        if (types == 2) {
          $(".product_id").show();
          $(".product_desc").hide();
        } else if (types == 3) {
           $(".product_id").hide();
          $(".product_desc").show();
        } else {
          $(".product_id").hide();
          $(".product_desc").hide();
        }
    }else{
      $(".shop_id").hide();
      $(".product_id").hide();
        if (types == 3) {
          $(".product_desc").show();
        } else {
          $(".product_desc").hide();
        }
    }
  }
  if (typeof FileReader == 'undefined') {
    $('.uploadimg').html('<h1>当前浏览器不支持FileReader接口</h1>');
  }

  $('.file-input').change(function() {
    var file = this.files[0];
    var reader = new FileReader();
    var that = $(this);
    reader.onload = function(e) {
      var img = $('#' + that.attr('data-id'));
      img.attr('src', e.target.result)
    }

    reader.readAsDataURL(file);
  })
</script>

<script type="text/javascript">
  var url = GetRequest();
  if(!url.id){
    $(".shop_id").show();
  
  }
  $(".jump").click(function(){
    if($(this).val()==1){
      $(".shop_id").hide();
      $(".appid").show();
      $(".product_id").hide();

    }else{
      var ops = mySelect.options;
      for(var i=0; i<ops.length; i++){
          if(ops[i].selected == true){
              var index = ops[i].value;
          }
      }
      if(index==3){
          $(".product_id").hide();
          $(".product_desc").show();
      }else if(index==2){
          $(".product_id").show();
          $(".product_desc").hide();
      }
      $(".shop_id").show();
      $(".appid").hide();
    }
  });
</script>